package com.wrench.elevator.base.user.service;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.wrench.elevator.base.user.dao.FaceDao;
import com.wrench.elevator.base.user.domain.Face;
import com.wrench.elevator.constant.Dict;
import com.wrench.elevator.constant.Result;
import com.wrench.elevator.exception.WrenchException;
import com.wrench.elevator.util.WrenchUtil;

/**
 * @Description: FaceService
 * @author yangchenfeng
 * @date Oct 15, 2017 1:24:01 PM
 * @version V1.0
 */
@Component
public class FaceService
{
    private static final Logger logger = LoggerFactory.getLogger(FaceService.class);
    
    @Resource
    private FaceDao dao;
    
    private static volatile int next_face_id = Dict.INIT_NEXT_FACE_ID;
    
    /**
     * 录脸
     * 
     * @throws WrenchException
     * */
    public int addFace(String user_id, String face_id, String identity, String face_feature, String age, String gender)
        throws WrenchException
    {
        
        // 判空
        if (WrenchUtil.hasNull(identity, face_feature, age,user_id,gender))
        {
            logger.error("==> param is not found , identity = {} , face_feature = {} , age = {} , user_id = {} , gender = {}  ",
                identity,
                face_feature,
                age,user_id,gender);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        // 判断年龄是否是整数
        if (!WrenchUtil.areInt(user_id, face_id, age))
        {
            logger.error("==> string is not int , user_id = {} , face_id = {} , age = {} ", user_id, face_id, age);
            throw new WrenchException(Result.STRING_IS_NOT_INT_CODE, Result.STRING_IS_NOT_INT_MESSAGE);
        }
        
        // 判断人脸身份是否符合要求
        if (!WrenchUtil.isVaildIdentity(identity))
        {
            logger.error("==> identity is not vaild , identity = {} ", identity);
            throw new WrenchException(Result.FACE_IDENTITY_IS_NOT_VAILD_CODE, Result.FACE_IDENTITY_IS_NOT_VAILD_MESSAGE);
        }
        
        // 判断性别
        if (!WrenchUtil.isVaildGender(gender))
        {
            logger.error("==> gender is not vaild , gender = {} ", gender);
            throw new WrenchException(Result.GENDER_IS_NOT_VAILD_CODE, Result.GENDER_IS_NOT_VAILD_MESSAGE);
        }
        
        Face face = new Face();
        face.setFace_id(Integer.valueOf(face_id));
        face.setUser_id(Integer.valueOf(user_id));
        face.setFace_feature(face_feature);
        face.setIdentity(identity);
        face.setGender(gender);
        face.setAge(Byte.valueOf(age));
        
        // 录脸
        int commit = -1;
        try
        {
            commit = dao.addFace(face);
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            logger.error("==> commit = dao.addFace(face) execute error ,face = {} , error = {}; ", face, e);
            throw new WrenchException(Result.DB_INSERT_ERROR_CODE, Result.DB_INSERT_ERROR_MESSAGE);
        }
        
        if (commit <= 0)
        {
            logger.error("==> commit = dao.addFace(face) execute success , but commit <=0 , commit = {} , face = {} ",
                commit,
                face);
            throw new WrenchException(Result.FACE_ADD_FACE_FAIL_CODE, Result.FACE_ADD_FACE_FAIL_MESSAGE);
        }
        
        logger.debug("==> commit = dao.addFace(face) execute success , commit = {} , face = {} ", commit, face);
        
        return commit;
    }
    
    /**
     * 获取下一个FACE_ID
     * 
     * @throws WrenchException
     * */
    public int getNextFaceId()
        throws WrenchException
    {
        if (next_face_id == Dict.INIT_NEXT_FACE_ID)
        {
            try
            {
                next_face_id = dao.getNextFaceId();
            }
            catch (Exception e)
            {
                logger.error("==> next_face_id=dao.getNextFaceId() execute error ,next_face_id = {} , error = {}; ",
                    next_face_id,
                    e);
                throw new WrenchException(Result.DB_SELECT_ERROR_CODE, Result.DB_SELECT_ERROR_MESSAGE);
            }
            
        }
        else
            next_face_id++;
        logger.debug("==>  next_face_id = {} ", next_face_id);
        return next_face_id;
    }
    
    /**
     * 根据人脸ID获取 身份和 用户ID
     * 
     * @throws WrenchException
     * */
    public Face getFaceByFaceId(String face_id)
        throws WrenchException
    {
        // 判空
        if (WrenchUtil.hasNull(face_id))
        {
            logger.error("==> param is not found , face_id = {} ", face_id);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        // 判断年龄是否是整数
        if (!WrenchUtil.areInt(face_id))
        {
            logger.error("==> string is not int , face_id = {} ", face_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_CODE, Result.STRING_IS_NOT_INT_MESSAGE);
        }
        
        Face face = null;
        try
        {
            face = dao.getFaceByFaceId(Integer.valueOf(face_id));
        }
        catch (Exception e)
        {
            logger.error("==> face=dao.getFaceByFaceId(Integer.valueOf(face_id)) execute error , face_id = {} , error = {}; ",
                face_id,
                e);
            throw new WrenchException(Result.DB_SELECT_ERROR_CODE, Result.DB_SELECT_ERROR_MESSAGE);
        }
        
        if (WrenchUtil.hasNull(face))
        {
            logger.error("==> face id is not exist , face_id = {} , face = {} ", face_id, face);
            throw new WrenchException(Result.FACE_ID_IS_NOT_EXIST_CODE, Result.FACE_ID_IS_NOT_EXIST_MESSAGE);
        }
        
        logger.debug("==> face=dao.getFaceByFaceId(Integer.valueOf(face_id)) execute success , face_id = {} , face = {}",
            face_id,
            face);
        
        return face;
        
    }
}
